﻿using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Fluent;
using Microsoft.Practices.EnterpriseLibrary.Common.Properties;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using System;
using System.Diagnostics;

namespace ZhongchenZhou.EnterpriseLibrary.Logging.DynamicsCrm2011.Configuration.Fluent
{
    public static class SendToDynamicsCrm2011ListenerExtensions
    {
        public static ILoggingConfigurationSendToDynamicsCrm2011TraceListener DynamicsCrm2011(this ILoggingConfigurationSendTo context, string listenerName)
        {
            if (string.IsNullOrEmpty(listenerName))
                throw new ArgumentException(Resources.ExceptionStringNullOrEmpty, "listenerName");

            return new SendToDynamicsCrm2011TraceListenerBuilder(context, listenerName);
        }

        private class SendToDynamicsCrm2011TraceListenerBuilder : SendToTraceListenerExtension, ILoggingConfigurationSendToDynamicsCrm2011TraceListener
        {
            DynamicsCrm2011TraceListenerData dynamicsCrm2011TraceListener;
            public SendToDynamicsCrm2011TraceListenerBuilder(ILoggingConfigurationSendTo context, string listenerName)
                : base(context)
            {
                dynamicsCrm2011TraceListener = new DynamicsCrm2011TraceListenerData
                {
                    Name = listenerName
                };

                base.AddTraceListenerToSettingsAndCategory(dynamicsCrm2011TraceListener);
            }


            public ILoggingConfigurationSendToDynamicsCrm2011TraceListener FormatWith(IFormatterBuilder formatBuilder)
            {
                if (formatBuilder == null) throw new ArgumentNullException("formatBuilder");

                FormatterData formatter = formatBuilder.GetFormatterData();
                dynamicsCrm2011TraceListener.Formatter = formatter.Name;
                LoggingSettings.Formatters.Add(formatter);

                return this;
            }

            public ILoggingConfigurationSendToDynamicsCrm2011TraceListener FormatWithSharedFormatter(string formatterName)
            {
                dynamicsCrm2011TraceListener.Formatter = formatterName;

                return this;
            }

            public ILoggingConfigurationSendToDynamicsCrm2011TraceListener WithTraceOptions(TraceOptions traceOptions)
            {
                dynamicsCrm2011TraceListener.TraceOutputOptions = traceOptions;

                return this;
            }

            public ILoggingConfigurationSendToDynamicsCrm2011TraceListener Filter(SourceLevels sourceLevel)
            {
                dynamicsCrm2011TraceListener.Filter = sourceLevel;

                return this;
            }

            public ILoggingConfigurationSendToDynamicsCrm2011TraceListener UseCrmConnection(string crmConnectionStringName)
            {
                dynamicsCrm2011TraceListener.CrmConnectionStringName = crmConnectionStringName;

                return this;
            }
        }
    }
}
